<?php

    class WZ_Route
    {
        static $_instance;
        public $module;
        public $controller;
        public $action;
        public $format;
        public $params;
		
		// singleton
		public static function &getInstance() 
		{
            if (!self::$_instance instanceof self) {
				self::$_instance = new self();
			}
            return self::$_instance;
		}
        
        public function setModule($module)
        {
            $this->module = $module;
        }
        
        public function getModule()
        {
            return $this->module;
        }
        
        public function setController($controller)
        {
            $this->controller = $controller;
        }
        
        public function getController()
        {
            return $this->controller;
        }
        
        public function setAction($action)
        {
            $this->action = $action;
        }
        
        public function getAction()
        {
            return $this->action;
        }
        
        public function setFormat($format)
        {
            $this->format = $format;
        }
        
        public function getFormat()
        {
            return $this->format;
        }
        
        public function setParam($key, $value)
        {
            $this->params[$key] = $value;
        }
        
        public function getParam($key)
        {
            if (isset($this->params[$key])) {
                return $this->params[$key];
            }
            return null;
        }
        
        public static function getRoutes()
        {
            return \WZ_Registry::get('confRoutes')->confRoutes;
        }
        
        public static function getRoute($uri)
        {
            $routes = self::getRoutes();
            foreach ($routes as $route) {
                $routeUri = $route['route'];
                if ($routeUri === $uri) {
                    return $route['defaults'];
                } else {
                    $routeUriRep = str_replace('[:any]', '.+', str_replace('[:num]', '[0-9]+', str_replace('[:alnum]', '[[:alnum:]]+', str_replace('[:segment]', '[^/]*', str_replace('[:alpha]', '[[:alpha:]]+', $routeUri)))));
                    if (preg_match('#^' . $routeUriRep . '$#', $uri)) {
                        $route = array_merge($route['defaults'], self::varUri($route, $uri));
                        return $route;
                    }
                }
            }
            return null;
        }
        
        public static function varUri($route, $uri)
        {
            $return = array();
            $r = str_replace(':any', '', $route['route']);
            $r = str_replace(':num', '', $r);
            $r = str_replace(':alnum', '', $r);
            $r = str_replace(':segment', '', $r);
            $r = str_replace(':alpha', '', $r);
            $tabUri = explode('[]', $r);
            foreach ($tabUri as $elem) {
                $uri = str_replace($elem, '%%', $uri);
            }
            $uri = substr($uri, 2, -2);
            $tabUri = explode('%%', $uri);
            for ($i = 0 ; $i < count($tabUri) ; $i++) {
                if (isset($route['defaults']['param' . ($i + 1)])) {
                    $var = self::cleanVar($tabUri[$i]);
                    $return[$route['defaults']['param' . ($i + 1)]] = $var;
                }
            }
            return $return;
        }
        
        public static function redirect($route)
        {
            header('Location: ' . \WZ_Url::getUrl($route));
            exit;
        }
        
        public static function isDefault($uri)
        {
            $routes = \WZ::get('confRoutes')->confRoutes;
            $defaultRoute = $routes['www_index']['route'];
            return ($uri == $defaultRoute) ? true : false;
        }
        
        public static function cleanVar($var)
        {
            $var = str_replace('.html', '', $var);
            $var = str_replace('.mb', '', $var);
            return $var;
        }
    }